home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Programming / amigatalk / general / Collection.st < prev    next >
Encoding:
Text File  |  2000-05-16  |  2.4 KB  |  99 lines

  1. Class Collection :Object
  2. [
  3.    addAll: aCollection
  4.      aCollection do: [:x | self add: x ]
  5. |
  6.    asArray
  7.       ^ Array new: self size ; replaceFrom: 1 to: self size with: self
  8. |
  9.    asBag
  10.       ^ Bag new addAll: self
  11. |
  12.    asSet
  13.       ^ Set new addAll: self
  14. |
  15.    asList
  16.       ^ List new addAllLast: self
  17. |
  18.    asString
  19.       ^ String new: self size ; replaceFrom: 1 to: self size with: self
  20. |
  21.    coerce: aCollection   ! newobj !
  22.       newobj <- self new.
  23.       aCollection do: [:x | newobj add: x].
  24.       ^ newobj
  25. |
  26.    collect: aBlock
  27.       ^ self inject: self class new
  28.              into: [:x :y | x add: (aBlock value: y). x ]
  29. |
  30.    deepCopy     ! newobj !
  31.       newobj <- List new .
  32.       self do: [:x | newobj addLast: x copy ].
  33.       ^ self coerce: newobj
  34. |
  35.    detect: aBlock
  36.       ^ self detect: aBlock
  37.       ifAbsent: [self error: 'no object found matching detect']
  38. |
  39.    detect: aBlock ifAbsent: exceptionBlock   
  40.       self do: [:x | (aBlock value: x) ifTrue: [^ x]].
  41.       ^ exceptionBlock value
  42. |
  43.    first
  44.       ^ self error: 'subclass should implement first'
  45. |
  46.    includes: anObject
  47.       self do: [:x | (x = anObject) ifTrue: [^ true]].
  48.       ^ false
  49. |
  50.    inject: thisValue into: binaryBlock     ! last !
  51.       last <- thisValue.
  52.       self do: [:x | last <- binaryBlock value: last value: x].
  53.       ^ last
  54. |
  55.    isEmpty
  56.       ^ (self size = 0)
  57. |
  58.    occurrencesOf: anObject
  59.       ^ self inject: 0
  60.                into: [:x :y | (y = anObject) 
  61.                       ifTrue: [x + 1]
  62.                       ifFalse: [x] ]
  63. |
  64.    printString
  65.       ^ ( self inject: self class printString , ' ('
  66.           into: [:x :y | x , ' ' , y printString]), ' )'
  67. |
  68.    reject: aBlock          
  69.       ^ self select: [:x | (aBlock value: x) not ]
  70. |
  71.    remove: oldObject
  72.       self remove: oldObject ifAbsent:
  73.                   [^ self error: 
  74.                      'attempt to remove object not found in collection' ].
  75.       ^ oldObject
  76. |
  77.    remove: oldObject ifAbsent: exceptionBlock
  78.       ^ (self includes: oldObject)
  79.          ifTrue: [self remove: oldObject]
  80.          ifFalse: exceptionBlock
  81. |
  82.    select: aBlock          
  83.       ^ self inject: self class new
  84.              into: [:x :y | (aBlock value: y) 
  85.                     ifTrue: [x add: y]. x]
  86. |
  87.    shallowCopy  ! newobj !
  88.       newobj <- List new.
  89.  
  90.       self do: [:x | newobj addLast: x].
  91.  
  92.       ^ self coerce: newobj
  93. |
  94.    size ! i !
  95.       i <- 0.
  96.       self do: [:x | i <- i + 1 ].
  97.       ^ i
  98. ]
  99.